package com.ruoyi.sys.security.service.impl;

import java.util.List;

import com.ruoyi.sys.security.domain.TbKeepWatch;
import com.ruoyi.sys.security.mapper.TbPatrollingRouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sys.security.domain.TbPatrollingRoute;
import com.ruoyi.sys.security.service.ITbPatrollingRouteService;

/**
 * 巡更路线Service业务层处理
 *
 * @author iuxin
 * @date 2022-05-25
 */
@Service
public class TbPatrollingRouteServiceImpl implements ITbPatrollingRouteService
{
    @Autowired
    private TbPatrollingRouteMapper tbPatrollingRouteMapper;

    /**
     * 查询巡更路线
     *
     * @param prId 巡更路线主键
     * @return 巡更路线
     */
    @Override
    public TbPatrollingRoute selectTbPatrollingRouteByPrId(Long prId)
    {
        return tbPatrollingRouteMapper.selectTbPatrollingRouteByPrId(prId);
    }

    /**
     * 查询巡更路线列表
     *
     * @param tbPatrollingRoute 巡更路线
     * @return 巡更路线
     */
    @Override
    public List<TbPatrollingRoute> selectTbPatrollingRouteList(TbPatrollingRoute tbPatrollingRoute)
    {
        return tbPatrollingRouteMapper.selectTbPatrollingRouteList(tbPatrollingRoute);
    }

    /**
     * 新增巡更路线
     *
     * @param tbPatrollingRoute 巡更路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbPatrollingRoute(TbPatrollingRoute tbPatrollingRoute)
    {
        int rows = tbPatrollingRouteMapper.insertTbPatrollingRoute(tbPatrollingRoute);
        insertTbKeepWatch(tbPatrollingRoute);
        return rows;
    }

    /**
     * 修改巡更路线
     *
     * @param tbPatrollingRoute 巡更路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbPatrollingRoute(TbPatrollingRoute tbPatrollingRoute)
    {
        tbPatrollingRouteMapper.deleteTbKeepWatchByKwId(tbPatrollingRoute.getPrId());
        insertTbKeepWatch(tbPatrollingRoute);
        return tbPatrollingRouteMapper.updateTbPatrollingRoute(tbPatrollingRoute);
    }

    /**
     * 批量删除巡更路线
     *
     * @param prIds 需要删除的巡更路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbPatrollingRouteByPrIds(Long[] prIds)
    {
        tbPatrollingRouteMapper.deleteTbKeepWatchByKwIds(prIds);
        return tbPatrollingRouteMapper.deleteTbPatrollingRouteByPrIds(prIds);
    }

    /**
     * 删除巡更路线信息
     *
     * @param prId 巡更路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbPatrollingRouteByPrId(Long prId)
    {
        tbPatrollingRouteMapper.deleteTbKeepWatchByKwId(prId);
        return tbPatrollingRouteMapper.deleteTbPatrollingRouteByPrId(prId);
    }

    /**
     * 新增巡更点配置信息
     *
     * @param tbPatrollingRoute 巡更路线对象
     */
    public void insertTbKeepWatch(TbPatrollingRoute tbPatrollingRoute)
    {
        List<TbKeepWatch> tbKeepWatchList = tbPatrollingRoute.getTbKeepWatchList();
        Long prId = tbPatrollingRoute.getPrId();
        if (StringUtils.isNotNull(tbKeepWatchList))
        {
            List<TbKeepWatch> list = new ArrayList<TbKeepWatch>();
            for (TbKeepWatch tbKeepWatch : tbKeepWatchList)
            {
                tbKeepWatch.setKwId(prId);
                list.add(tbKeepWatch);
            }
            if (list.size() > 0)
            {
                tbPatrollingRouteMapper.batchTbKeepWatch(list);
            }
        }
    }
}
